<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style media="screen">
    canvas{
      border:1px solid red;
    }
  </style>
</head>
<body>
  <canvas id="canvas" width="800" height="600"></canvas>
  <script src="js/underscore-min.js"></script>
  <script>
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');

    var ballArr = [];
    function Ball(x,y){
      this.x = x;
      this.y = y;
      this.color = 'skyblue';
      this.r = 30;


      this.dx = Math.random() * 10 - 5;
      this.dy = Math.random() * 10 - 5;
      this.dr = Math.random() * 0.2;
      ballArr.push(this);
    }

    Ball.prototype = {
      update:function(){
        this.x += this.dx;
        this.y += this.dy;
        this.r -= this.dr;

        if(this.r <=0){
          ballArr = _.without(ballArr,this);
        }
      },
      render:function(){
        ctx.beginPath();
        ctx.arc(this.x,this.y,this.r,2*Math.PI,false);
        ctx.closePath();
        ctx.fillStyle = this.color;
        ctx.fill();
      }
    };




    new Ball(100,100);
    new Ball(100,100);
    new Ball(100,100);
    new Ball(100,100);
    new Ball(100,100);
    setInterval(function(){
      ctx.clearRect(0,0,canvas.width,canvas.height);
      for(var i = 0; i < ballArr.length; i++){
        ballArr[i] && ballArr[i].update();
        ballArr[i] && ballArr[i].render();
      }


    },20);



    canvas.addEventListener('mousemove',function(ev){
      var x = ev.clientX;
      var y = ev.clientY;
      new Ball(x,y);
    })
  </script>
</body>
</html>
